import datetime
import os
import sys
# from webdriver_manager.microsoft import EdgeChromiumDriverManager
# from selenium.webdriver.edge.service import Service
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
# from webdriver_manager.chrome import ChromeDriverManager
from webdrivermanager_cn import ChromeDriverManager
class Driver:
    """管理 Chrome WebDriver 并提供截图功能"""
    driver = None
    def __init__(self):
        """初始化 Chrome WebDriver 并设置选项"""
        options = webdriver.ChromeOptions()
        options.page_load_strategy = 'eager'
        # options.add_argument('-headless')# 运行无头模式，可选
        Chrome_driver_path = ChromeDriverManager().install()
        self.driver = webdriver.Chrome(service=Service(Chrome_driver_path),options=options)

    def getScreenShot(self):
        """
                截取当前页面的截图，并保存在 images 目录下
                """
        # 生成以当前日期命名的目录
        dirname = datetime.datetime.now().strftime('%Y-%m-%d')
        screenshot_dir = os.path.join(os.path.dirname(os.getcwd()), 'images', dirname)
        # 如果截图目录不存在，则创建
        if not os.path.exists(screenshot_dir):
            os.makedirs(screenshot_dir)
            # 根据方法名和时间戳生成唯一的文件名
        filename = sys._getframe().f_back.f_code.co_name + "-" + datetime.datetime.now().strftime('%Y-%m-%d-%H%M%S') + ".png"
        # 保存截图
        screenshot_path = os.path.join(screenshot_dir, filename)
        self.driver.save_screenshot(screenshot_path)# 返回截图的保存路径
# 创建Driver实例
Driver = Driver()